package newx.com.control.tab
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import newx.com.NxUIComponent;
	import newx.com.control.ITabBar;
	import newx.com.control.button.NxButton;
	import newx.com.control.button.NxButtonState;
	
	public class NxTab extends NxUIComponent implements ITabBar
	{
		/**
		 * 	bar垂直排列
		 */	
		public static var VERTICAL : String 		= "vertical";
		
		/**
		 * 	bar水平排列
		 */
		public static var HORIZONTAL : String		=	"horizontal";
		
		private var _tabContainer		: Sprite;
		
		private var _dataProvider		: Array		=	[];
		
		private var _skin 				: String;
		
		private var _autoScale 		: Boolean;
		
		private var _defaultLabelColor	: uint		=	0xFFC861;
		private var _seletedLabelColor	: uint		=	0xFF1111;
		
		private var _defaultWidth		: int		=	80;
		
		private var _defaultHeight		: int		=	27;
		
		private var _offsizeX			: int;
		
		private var _offsizeY			: int;
		
		private var _selected			: int		=	0;
		
		private var _gap				: int		=	-8;
		
		private var _itemLayout		: String;
		
		private var _horizontalLayout	: String;
		
		private var _verticalLayout	: String;
		
		
		public function NxTab()
		{
			super();
			init();
		}
		
		private function init() : void
		{
			_tabContainer = new Sprite();
			addChild(_tabContainer);
			mouseEnabled = false
			_tabContainer.mouseEnabled = false;
		}
		
		private function draw() : void
		{
			if(_skin == null || _skin == "") return;
			
			clearAll();
			
			var leng : int = _dataProvider.length;
			if(leng <= 0) return;
			
			var item : Object;
			var btn : NxButton;
			
			for(var i : int = 0; i < leng; i++)
			{
				item = _dataProvider[i];
				btn = new NxButton();
				btn.skin = _skin;
				btn.label = item.label;
				btn.removeListener();
				_tabContainer.addChild(btn);
				item.btn = btn;
				item.index = i;
				
				if(item.editable)
				{
					btn.enabled	=	true;
					addListener( btn );
				}
				else
				{
					btn.enabled	=	false;
				}
			}
			addChild(_tabContainer);
			
			updataBar()
			sortLayer();
		}
		
		private function addListener( btn : NxButton ):void
		{
			btn.addEventListener( MouseEvent.CLICK, onClick );
			btn.addEventListener(MouseEvent.MOUSE_OVER, onOver);
			btn.addEventListener(MouseEvent.MOUSE_OUT, onOut);
		}
		
		/**
		 *	更新位置(坐标)
		 */
		private function updataLocation() : void
		{
			var bar : NxButton;
			var location : int;
			for(var i : int = 0; i < _dataProvider.length; i++)
			{
				bar = _dataProvider[i].btn;
				if(bar == null) break;
				//垂直排列
				if(_itemLayout == VERTICAL)
				{
					bar.x = 0;
					bar.y = location;
					location = location + bar.height + _gap;
				}else//水平
				{
					bar.y = 0;
					bar.x = location;
					location = location + bar.width + _gap;
				}
			}
		}
		
		/**
		 * tab按钮层次排序
		 */
		private function sortLayer():void
		{
			var index : int = _selected;
			var leng : int = _dataProvider.length;
			
			var childAtList : Array = [];
			var obj : Object;
			//往两边扩散
			for(var i : int = 0; i < leng; i+=1){
				obj = {};
				obj["childAt"] = Math.abs(index - i);
				obj["index"] = i;
				obj["editable"] = _dataProvider[i].editable;
				childAtList.push(obj);
			}
			childAtList.sortOn("childAt",Array.NUMERIC | Array.DESCENDING);
			
			var bar : NxButton;
			for each(var tab : Object in childAtList){
				bar = _dataProvider[tab["index"]].btn;
				_tabContainer.addChild(bar);
				//是否可以选中
				if(!tab.editable)
				{
					bar.currentState = NxButtonState.UNALLOWED;
					continue;
				}
				if(tab["index"] == _selected)
				{
					bar.labelColor = _seletedLabelColor;
					bar.currentState = NxButtonState.DOWN;
				}else{
					bar.labelColor = _defaultLabelColor;
					bar.currentState = NxButtonState.NORMAL;
				}
			}
		}
		
		/**
		 * 更新bar中属性
		 */
		private function updataBar(type:String = "all", value:Object = null) : void
		{
			var btn : NxButton;
			for each(var bar : Object in _dataProvider)
			{
				btn = bar.btn;
				if(type == "all")
				{
					btn.htmlStrByBorder = bar.label;
					btn.labelColor = _defaultLabelColor;
					btn.width = bar.width;
					btn.height = bar.height;
					btn.offsizeX = _offsizeX;
					btn.offsizeY = _offsizeY;
					btn.horizontalLayout = _horizontalLayout;
					btn.verticalLayout = _verticalLayout;
				}
				else if(value != null)
				{
					btn[type] = value;
				}
			}
			
			updataLocation();
		}
		
		/**
		 *	清空 
		 */
		private function clearAll() : void
		{
			while(_tabContainer.numChildren > 0)
			{
				_tabContainer.removeChildAt(0);
			}
		}
		
		private function onOver(evt : MouseEvent):void
		{
			var btn : NxButton = evt.currentTarget as NxButton;
			
			for each(var bar : Object in _dataProvider)
			{
				if(bar.btn == btn && bar.index != _selected )
				{
					btn.currentState = NxButtonState.OVER;
					return;
				}
			}
		}
		
		private function onOut(evt : MouseEvent):void
		{
			var btn : NxButton = evt.currentTarget as NxButton;
			
			if( btn.currentState == NxButtonState.OVER )
			{
				btn.currentState = NxButtonState.NORMAL;
			}
		}
		
		/**
		 *	点击tab 
		 * @param evt
		 * 
		 */
		private function onClick(evt : MouseEvent) : void
		{
			var btn : NxButton = evt.currentTarget as NxButton;
			if(btn == null)return;
			
			//查找点击的索引
			var index : int = 0;
			for each(var bar : Object in _dataProvider)
			{
				if(bar.btn == btn)
				{
					index = bar.index;
					break;
				}
			}
			//点击同一个，retur
			if(index == _selected) return;
			
			selected = index;
		}
		
		public function get skin():String
		{
			return _skin;
		}
		
		public function set skin(val:String):void
		{
			_skin = val;
			
			draw();
		}
		
		public function get autoScale():Boolean
		{
			return _autoScale;
		}
		
		public function set autoScale(val:Boolean):void
		{
			_autoScale = val;
			
			updataLocation();
		}
		
		public function get defaultLabelColor():uint
		{
			return _defaultLabelColor;
		}
		
		public function set defaultLabelColor(val:uint):void
		{
			_defaultLabelColor = val;
			
			updataBar("labelColor", _defaultLabelColor);
		}
		
		public function get defaultWidth():uint
		{
			return _defaultWidth;
		}
		
		public function set defaultWidth(val:uint):void
		{
			_defaultWidth = val;
			
			for each(var item : Object in _dataProvider)
			{
				item.width = _defaultWidth;
			}
			updataBar("width", _defaultWidth);
		}
		
		public function get defaultHeight():uint
		{
			return _defaultHeight;
		}
		
		public function set defaultHeight(val:uint):void
		{
			_defaultHeight = val;
			
			for each(var item : Object in _dataProvider)
			{
				item.height = _defaultHeight;
			}
			updataBar("height", _defaultHeight);
		}
		
		public function setBarWidth(index:int, val:uint):void
		{
			if(index < 0 || index > _dataProvider.length) return;
			_dataProvider[index].width = val;
			
			draw();
		}
		
		public function setBarHeight(index:int, val:uint):void
		{
			if(index < 0 || index > _dataProvider.length) return;
			_dataProvider[index].height = val;
			
			draw();
		}
		
		public function getBarWidth(index:int):uint
		{
			if(index < 0 || index > _dataProvider.length) return 0;
			
			return _dataProvider[index].width;
		}
		
		public function getBarHeight(index:int):uint
		{
			if(index < 0 || index > _dataProvider.length) return 0;
			
			return _dataProvider[index].height;
		}
		
		public function get horizontalLayout():String
		{
			return _horizontalLayout;
		}
		
		public function set horizontalLayout(val:String):void
		{
			_horizontalLayout = val;
			
			updataBar("horizontalLayout", _horizontalLayout);
		}
		
		public function get verticalLayout():String
		{
			return _verticalLayout;
		}
		
		public function set verticalLayout(val:String):void
		{
			_verticalLayout = val;
			
			updataBar("verticalLayout", _verticalLayout);
		}
		
		public function get offsizeX():int
		{
			return _offsizeX;
		}
		
		public function set offsizeX(val:int):void
		{
			_offsizeX = val;
			
			updataBar("offsizeX", _offsizeX);
		}
		
		public function get offsizeY():int
		{
			return _offsizeY;
		}
		
		public function set offsizeY(val:int):void
		{
			_offsizeY = val;
			
			updataBar("offsizeY", _offsizeY);
		}
		
		public function get selected():int
		{
			return _selected;
		}
		
		public function set selected(val:int):void
		{
			//看是否可以点击
			if(_dataProvider[val] !=null)
			{
				if(!_dataProvider[val].editable) return;
			}
			
			_selected = val;
			
			//抛出选中事件
			var evt : NxTabEvent = new NxTabEvent(NxTabEvent.TAB_CLICK);
			evt.seleted = _selected;
			evt.item = _dataProvider[_selected];
			this.dispatchEvent(evt);
			
			//重新排层次
			sortLayer();
		}
		
		public function get count():int
		{
			return _dataProvider.length;
		}
		
		public function get gap():int
		{
			return _gap;
		}
		
		public function set gap(val:int):void
		{
			_gap = val;
			
			updataLocation();
		}
		
		public function get itemLayout():String
		{
			return _itemLayout;
		}
		
		public function set itemLayout(val:String):void
		{
			_itemLayout = val;
			
			updataLocation();
		}
		
		public function addItem(label:String, index:int=-1, data:Object=null, editable:Boolean=true):void
		{
			var item : Object = {};
			item.label = label;
			item.data = data;
			item.editable = editable;
			item.width = _defaultWidth;
			item.height = _defaultHeight;
			
			_dataProvider.splice(index == -1 ? _dataProvider.length : index, 0, item);
			
			draw();
		}
		
		public function updataItem(label:String, index:int=-1, data:Object=null, editable:Boolean=true):void
		{
			var item : Object = _dataProvider[ index ?  index:_dataProvider.length];
			
			if(item != null)
			{
				item.label = label;
				item.data = data;
				item.editable = editable;
			}
			
			draw();
		}
		
		public function deleteItem(index:int):void
		{
			_dataProvider.splice(index, 1);
			
			draw();
		}
		
		
		/**
		 *	废弃接口，推荐使用seleted接口 
		 * @param val
		 * 
		 */
		public function selectTabByIndex(val:uint):void
		{
			selected = val;
		}
		
		public function get dataProvider():Array
		{
			return _dataProvider;
		}
		
		/**
		 * 全部设置
		 */
		public function set dataProvider(value:Array):void
		{
			_dataProvider = [];
			for each(var tab : Object in value)
			{
				var item : Object = {};
				item.label = tab.label;
				item.data = tab.data;
				item.editable = tab.hasOwnProperty("editable") ? tab.editable : true;
				item.width = _defaultWidth;
				item.height = _defaultHeight;
				
				_dataProvider.push(item);
			}
			
			draw();
		}
		
		public function clearItems():void
		{
			_selected		=	0;
			_dataProvider 	= 	[];			
			draw();
		}
		
		public function get seletedLabelColor():uint
		{
			return _seletedLabelColor;
		}
		
		public function set seletedLabelColor(value:uint):void
		{
			_seletedLabelColor = value;
			
			updataLocation();
		}
		
	}
}